<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-bar">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="部门">
          <department-tree-select
            v-model="searchForm.department"
            placeholder="请选择部门"
            clearable
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item label="岗位">
          <el-input
            v-model="searchForm.station"
            placeholder="请输入岗位"
            clearable
            prefix-icon="el-icon-office-building"
            style="width: 180px"
          ></el-input>
        </el-form-item>
        <el-form-item label="工号">
          <el-input
            v-model="searchForm.jobNumber"
            placeholder="请输入工号"
            clearable
            prefix-icon="el-icon-user"
            style="width: 180px"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input
            v-model="searchForm.name"
            placeholder="请输入姓名"
            clearable
            prefix-icon="el-icon-user"
            style="width: 180px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" icon="el-icon-search">查询</el-button>
          <el-button @click="resetSearch" icon="el-icon-refresh">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
      @row-click="handleRowClick"
    >
      <el-table-column
        prop="name"
        label="姓名"
        min-width="80"
        align="left"
      >
      </el-table-column>
      <el-table-column
        prop="jobNumber"
        label="工号"
        min-width="100"
        align="left"
      ></el-table-column>
      <el-table-column
        prop="department"
        label="部门"
        min-width="100"
        align="left"
      ></el-table-column>
      <el-table-column
        prop="station"
        label="岗位"
        min-width="100"
        align="left"
      ></el-table-column>
      <el-table-column
        prop="phone"
        label="电话"
        min-width="120"
        align="left"
      ></el-table-column>
      <el-table-column
        label="状态"
        width="100"
        align="left"
      >
        <template slot-scope="scope">
          <el-tag :type="scope.row.state === 1 ? 'success' : 'warning'" size="mini">
            {{ scope.row.state === 1 ? '正式' : '实习' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="是否启用"
        width="100"
        align="left"
      >
        <template slot-scope="scope">
          <el-tag :type="scope.row.isDeleted === 0 ? 'success' : 'danger'" size="mini">
            {{ scope.row.isDeleted === 0 ? '启用' : '已删除' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="180"
        fixed="right"
        align="left"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            class="delete-btn"
            @click.stop="handleDelete(scope.row)"
            :disabled="scope.row.isDeleted === 1"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 详细信息对话框 -->
    <el-dialog
      title="员工详细信息"
      :visible.sync="detailDialogVisible"
      width="800px"
      :close-on-click-modal="false"
      :modal="true"
      :append-to-body="true"
      :modal-append-to-body="false"
      :destroy-on-close="true"
      class="employee-dialog"
    >
      <!-- 基础信息 -->
      <div class="detail-section">
        <div class="section-title">基础信息</div>
        <el-descriptions :column="3" border>
          <el-descriptions-item label="姓名">{{ detailData.name }}</el-descriptions-item>
          <el-descriptions-item label="性别">
            {{ detailData.staffInfoVO && detailData.staffInfoVO.gender === 0 ? '男' : '女' }}
          </el-descriptions-item>
          <el-descriptions-item label="电话">{{ detailData.phone }}</el-descriptions-item>
          <el-descriptions-item label="身份证号">{{ detailData.staffInfoVO && detailData.staffInfoVO.idNumber || '--' }}</el-descriptions-item>
          <el-descriptions-item label="民族">{{ detailData.staffInfoVO && detailData.staffInfoVO.nation || '--' }}</el-descriptions-item>
          <el-descriptions-item label="政治面貌">
            {{ getPoliticText(detailData.staffInfoVO && detailData.staffInfoVO.politic) }}
          </el-descriptions-item>
          <el-descriptions-item label="婚姻状况">
            {{ detailData.staffInfoVO && detailData.staffInfoVO.marital === 1 ? '已婚' : '未婚' }}
          </el-descriptions-item>
          <el-descriptions-item label="户籍">
            {{ getCensusText(detailData.staffInfoVO && detailData.staffInfoVO.census) }}
          </el-descriptions-item>
          <el-descriptions-item label="住址">{{ detailData.staffInfoVO && detailData.staffInfoVO.address }}</el-descriptions-item>
          <el-descriptions-item label="学历">{{ detailData.staffInfoVO && detailData.staffInfoVO.education }}</el-descriptions-item>
          <el-descriptions-item label="入职日期">{{ detailData.staffInfoVO && detailData.staffInfoVO.workDate || '--' }}</el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :type="detailData.staffInfoVO && detailData.staffInfoVO.state === 1 ? 'success' : 'info'" size="mini">
              {{ detailData.staffInfoVO && detailData.staffInfoVO.state === 1 ? '在职' : '离职' }}
            </el-tag>
          </el-descriptions-item>
        </el-descriptions>
      </div>

      <!-- 岗位信息 -->
      <div class="detail-section">
        <div class="section-title">岗位信息</div>
        <el-descriptions :column="3" border>
          <el-descriptions-item label="工号">{{ detailData.jobNumber }}</el-descriptions-item>
          <el-descriptions-item label="部门">{{ detailData.department }}</el-descriptions-item>
          <el-descriptions-item label="岗位">{{ detailData.station }}</el-descriptions-item>
          <el-descriptions-item label="员工状态">
            <el-tag :type="detailData.state === 1 ? 'success' : 'warning'" size="mini">
              {{ detailData.state === 1 ? '正式' : '实习' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="是否启用">
            <el-tag :type="detailData.isDeleted === 0 ? 'success' : 'danger'" size="mini">
              {{ detailData.isDeleted === 0 ? '启用' : '已删除' }}
            </el-tag>
          </el-descriptions-item>
        </el-descriptions>
      </div>

      <!-- 合同信息 -->
      <div class="detail-section">
        <div class="section-title">合同信息</div>
        <el-descriptions :column="3" border>
          <el-descriptions-item label="合同类型">
            {{ getContractText(detailData.staffContractVO && detailData.staffContractVO.contract) }}
          </el-descriptions-item>
          <el-descriptions-item label="薪资">{{ detailData.staffContractVO && detailData.staffContractVO.compensation }}</el-descriptions-item>
          <el-descriptions-item label="合同期限">{{ detailData.staffContractVO && detailData.staffContractVO.duration }}年</el-descriptions-item>
          <el-descriptions-item label="签订日期">{{ detailData.staffContractVO && detailData.staffContractVO.signDate || '--' }}</el-descriptions-item>
          <el-descriptions-item label="开始日期">{{ detailData.staffContractVO && detailData.staffContractVO.startDate || '--' }}</el-descriptions-item>
          <el-descriptions-item label="到期日期">{{ detailData.staffContractVO && detailData.staffContractVO.expirationDate || '--' }}</el-descriptions-item>
          <el-descriptions-item label="合同状态">
            <el-tag :type="detailData.staffContractVO && detailData.staffContractVO.state === 0 ? 'success' : 'danger'" size="mini">
              {{ detailData.staffContractVO && detailData.staffContractVO.state === 0 ? '有效' : '无效' }}
            </el-tag>
          </el-descriptions-item>
        </el-descriptions>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="detailDialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 分页器 -->
    <pagination
      :current-page.sync="pagination.current"
      :page-size.sync="pagination.size"
      :total="pagination.total"
      @pagination-change="fetchEmployeeData"
    />
  </div>
</template>

<script>
import DepartmentTreeSelect from '@/components/common/DepartmentTreeSelect.vue'
import Pagination from '@/components/common/Pagination.vue'

export default {
  name: 'Employees',
  components: {
    DepartmentTreeSelect,
    Pagination
  },
  data() {
    return {
      loading: false,
      
      searchForm: {
        department: '',
        station: '',
        jobNumber: '',
        name: ''
      },
      tableData: [],
      pagination: {
        current: 1,
        size: 10,
        total: 0
      },
      detailDialogVisible: false,
      currentRow: {},
      detailData: {}
    }
  },
  created() {
    this.fetchEmployeeData()
  },
  methods: {
    // 获取员工数据
    async fetchEmployeeData() {
      try {
        this.loading = true
        // 构造与后端接口匹配的参数结构
        const params = {
          // Page 对象
            current: this.pagination.current,
            size: this.pagination.size,
          // StaffPosition 对象
            department: this.searchForm.department || null,
            station: this.searchForm.station || null,
            jobNumber: this.searchForm.jobNumber || null,
            name: this.searchForm.name || null

        }

        console.log('请求参数：', params)

        const response = await this.$axios.get('/employee/staffPosition', { params })
        
        if (response && response.result === 200) {
          console.log('获取员工数据：', response.data)
          const { records, total, size, current } = response.data
          this.tableData = records
          this.pagination = {
            total,
            size,
            current
          }
        } else {
          this.$message.error('获取员工数据失败')
        }
      } catch (error) {
        console.error('获取员工数据出错：', error)
        this.$message.error('获取员工数据失败')
      } finally {
        this.loading = false
      }
    },

    // 搜索
    handleSearch() {
      this.pagination.current = 1
      this.fetchEmployeeData()
    },

    // 重置搜索
    resetSearch() {
      this.searchForm = {
        department: '',
        station: '',
        jobNumber: '',
        name: ''
      }

      this.handleSearch()
    },

    // 处理行点击
    handleRowClick(row) {
      this.handleView(row)
    },

    // 查看员工详情
    async handleView(row) {
      try {
        const response = await this.$axios.get(`/employee/staffPosition/${row.id}`)
        if (response && response.result === 200) {
          this.detailData = response.data
          this.detailDialogVisible = true
        } else {
          this.$message.error('获取详情失败')
        }
      } catch (error) {
        console.error('获取详情失败：', error)
        this.$message.error('获取详情失败')
      }
    },

    // 删除员工
    handleDelete(row) {
      this.$confirm('确认删除该员工吗？此操作不可恢复', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          // TODO: 调用删除 API
          this.$message.success('删除成功')
          this.fetchEmployeeData()
        } catch (error) {
          console.error('删除失败：', error)
          this.$message.error('删除失败')
        }
      }).catch(() => {})
    },

    // 获取政治面貌文本
    getPoliticText(politic) {
      const politicMap = {
        '0': '群众',
        '1': '党员',
        '2': '团员'
      }
      return politicMap[politic] || '--'
    },

    // 获取户籍文本
    getCensusText(census) {
      const censusMap = {
        '0': '本地',
        '1': '外地'
      }
      return censusMap[census] || '--'
    },

    // 获取合同类型文本
    getContractText(contract) {
      const contractMap = {
        '0': '劳动合同',
        '1': '实习合同',
        '2': '劳务合同'
      }
      return contractMap[contract] || '--'
    }
  }
}
</script>

<style scoped>
/* 容器样式 */
.employee-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  min-height: calc(100vh - 120px);
}

/* 搜索区域样式 */
.search-bar {
  margin-bottom: 20px;
}

.search-form {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0;
}

/* 表格相关样式 */
.name-column {
  display: flex;
  align-items: center;
  gap: 8px;
}

.name-column i {
  color: #409EFF;
}

.delete-btn {
  color: #F56C6C;
}

.delete-btn:hover {
  color: #f78989;
}

/* 详情对话框样式 */
.detail-section {
  margin-bottom: 20px;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 15px;
  padding-left: 10px;
  border-left: 4px solid #409EFF;
}

.dialog-footer {
  text-align: right;
  margin-top: 20px;
}

</style> 